package cheme.ProcessEditor.Parts.ConsoleView;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;

import cheme.ProcessEditor.Figures.ConsoleViewFig;
import cheme.ProcessEditor.Figures.SolvabilityStatusFig;
import cheme.ProcessEditor.Parts.INonResizablePart;
import cheme.ProcessEditor.Parts.IResizeablePart;
import cheme.ProcessEditor.Parts.PropertyAwarePart;
import cheme.ProcessEditor.Parts.Equations.PolicyEquationsLayout;
import cheme.ProcessEditor.Parts.Equations.ccc_aaa_PolicyEquations;
import cheme.ProcessModel.ConsoleView;

public class PartConsoleView extends PropertyAwarePart implements IResizeablePart //implements INonResizablePart
{
	private int oldWidth = 1100;
	private int oldHeight = 205;
	private Point oldBounds = new Point(0,660);	
	

	@Override
	protected ConsoleViewFig createFigure() {
				
		ConsoleViewFig fig = new ConsoleViewFig();				
		return fig;
		
	}

	@Override
	public ConsoleViewFig getFigure() {
		return (ConsoleViewFig)super.getFigure();
	}
	@Override
	public ConsoleView getModel() {
		return (ConsoleView)super.getModel();
	}
	
	@Override
	protected void createEditPolicies() {
		//installEditPolicy(EditPolicy.LAYOUT_ROLE, new PolicyEquationsLayout());
		installEditPolicy(EditPolicy.COMPONENT_ROLE, new ccc_aaa_PolicyEquations());
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new PolicyEquationsLayout());
		
	}
	
//	PA Change to call Fig's methods
	protected void handleTextChange(PropertyChangeEvent evt)
	{
		ConsoleViewFig fig = (ConsoleViewFig)super.getFigure();
		String message = getModel().getMessage();
		fig.setMessage(message);
	}
	
	// Require for moving the figure, by drag and drop
	protected void handleBoundsChange(PropertyChangeEvent evt)
	{
		refreshVisuals();
		super.handleBoundsChange(evt);
	}
	
	protected void handleClearChange(PropertyChangeEvent evt)
	{
		ConsoleViewFig fig = (ConsoleViewFig)super.getFigure();
		fig.clearAll();
	}
	
	/**
	 * Reset the layout constraint, and revalidate the content pane
	 * This resizes the window vertically and also moves the window
	 */
	protected void refreshVisuals()
	{
		Point loc = getModel().getLocation();
		Rectangle constraint = new Rectangle(loc.x,loc.y,oldWidth,oldHeight);
		if (!loc.equals(oldBounds))
		{
			int newHeight =  oldBounds.y - loc.y;
							
			oldHeight += newHeight;
			constraint.height = oldHeight;			
			oldBounds.x = loc.x;
			oldBounds.y = loc.y;		
		}		

	    ((GraphicalEditPart)getParent()).setLayoutConstraint(this, getFigure(), constraint);
		
	}
	

}
